package com.example.student.utils;

/**
 * 加密/解密工具类
 */
public class Base64Encoder {

    /**
     * 获取base64编码加密
     * @param s
     * @return
     */
    public static String getBase64(String s) {
        return (new sun.misc.BASE64Encoder()).encode(s.getBytes());
    }
    /**
     * 获取base64编码解密
     * @param s
     * @return
     */
    public static String getFromBase64(String s) {
        if (s == null){
            return null;
        }
        sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
        try {
            byte[] b = decoder.decodeBuffer(s);
            return new String(b);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 加密
     * @param ming
     * @return
     */
    public static String doEncrypt(String ming){
        return Base64Encoder.getBase64(Base64Encoder.getBase64(Base64Encoder.getBase64(ming)));
    }

    /**
     * 解密
     * @param an
     * @return
     */
    public static String doDecrypt(String an){
        return Base64Encoder.getFromBase64(Base64Encoder.getFromBase64(Base64Encoder.getFromBase64(an)));
    }
}
